var wordBreak = function(s, wordDict) {
  const len = s.length;
  const dp = new Array(len + 1).fill(false);

  dp[0] = true;


  // 考虑排列顺序 --- 先背包 在物品
  for (let i = 0; i <= len; i++) {
    for (let j = 0; j < wordDict.length; j++) {
      if (i >= wordDict[j].length) {
        if (dp[i - wordDict[j].length] && s.slice(i -wordDict[j].length, i) === wordDict[j]) {
          dp[i] = true;
        }
      }
    }

  }

  return dp[len];
};

const s = "applepenapple", wordDict = ["apple","pen"]
console.log(wordBreak(s, wordDict)); // Output: true